package org.flowers.unbeaten.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.flowers.unbeaten.logging.aop.annotation.Logging;
import org.flowers.unbeaten.logging.aop.enums.BusinessType;
import org.flowers.unbeaten.base.entity.Menu;
import org.flowers.unbeaten.service.IMenuService;
import org.flowers.unbeaten.util.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author RyanWang
 * @since 2021-07-15
 */
@Api(tags = "后台系统-菜单管理")
@RestController
@RequestMapping("/menu/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有菜单接口")
    @Logging(title = "菜单管理", describe = "获取所有菜单", type = BusinessType.QUERY)
    @RequiresPermissions("menu:menu:list")
    public DataResult list() {
        return DataResult.success(menuService.selectAllMenus());
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增菜单接口")
    @Logging(title = "菜单管理", describe = "新增菜单", type = BusinessType.ADD)
    @RequiresPermissions("menu:menu:add")
    public DataResult add(@RequestBody Menu menu) {
        menuService.add(menu);
        return DataResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑菜单接口")
    @Logging(title = "菜单管理", describe = "编辑菜单", type = BusinessType.EDIT)
    @RequiresPermissions("menu:menu:edit")
    public DataResult edit(@RequestBody Menu menu) {
        menuService.edit(menu);
        return DataResult.success();
    }

    @DeleteMapping("/delete/{ids}")
    @ApiOperation(value = "删除菜单接口")
    @Logging(title = "菜单管理", describe = "删除菜单", type = BusinessType.REMOVE)
    @RequiresPermissions("menu:menu:delete")
    public DataResult delete(@PathVariable List<String> ids) {
        menuService.delete(ids);
        return DataResult.success();
    }

    @GetMapping("/permission/{menuId}")
    @ApiOperation(value = "菜单详情接口")
    @Logging(title = "菜单管理", describe = "菜单详情", type = BusinessType.QUERY)
    @RequiresPermissions("menu:menu:detail")
    public DataResult detail(@PathVariable String menuId) {
        return DataResult.success(menuService.detail(menuId));
    }

    @GetMapping("/tree/all")
    @ApiOperation(value = "获取所有菜单树接口")
    @Logging(title = "菜单管理", describe = "获取所有菜单树", type = BusinessType.QUERY)
    @RequiresPermissions(value = {"menu:menu:add", "menu:menu:edit"}, logical = Logical.OR)
    public DataResult getAllTree() {
        return DataResult.success(menuService.getAllTree());
    }

    @GetMapping("/tree")
    @ApiOperation(value = "获取所有菜单树（不包含按钮）接口")
    @Logging(title = "菜单管理", describe = "获取所有菜单树（不包含按钮）", type = BusinessType.QUERY)
    @RequiresPermissions(value = {"menu:menu:add", "menu:menu:edit"}, logical = Logical.OR)
    public DataResult getAllTreeExceptButton() {
        return DataResult.success(menuService.getAllTreeExceptButton());
    }

}

